/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年2月6日         Root.Lu
 */

package com.coocaa.framework.system;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.coocaa.x.framework.data.JObject;

public class JoyStick {
    public static class JoyStickDevice extends JObject {
        public static final String KEYCODE_DPAD_UP = "dpad_up";
        public static final String KEYCODE_DPAD_DOWN = "dpad_down";
        public static final String KEYCODE_DPAD_LEFT = "dpad_left";
        public static final String KEYCODE_DPAD_RIGHT = "dpad_right";

        public static final String KEYCODE_RT = "rt";
        public static final String KEYCODE_LT = "lt";
        public static final String KEYCODE_RB = "rb";
        public static final String KEYCODE_LB = "lb";

        public static final String KEYCODE_A = "a";
        public static final String KEYCODE_B = "b";
        public static final String KEYCODE_X = "x";
        public static final String KEYCODE_Y = "y";

        public static final String KEYCODE_LSTICK_UP = "lstick_up";
        public static final String KEYCODE_LSTICK_DOWN = "lstick_down";
        public static final String KEYCODE_LSTICK_LEFT = "lstick_left";
        public static final String KEYCODE_LSTICK_RIGHT = "lstick_right";
        public static final String KEYCODE_LSTICK_CENTER = "lstick_center";

        public static final String KEYCODE_RSTICK_UP = "rstick_up";
        public static final String KEYCODE_RSTICK_DOWN = "rstick_down";
        public static final String KEYCODE_RSTICK_LEFT = "rstick_left";
        public static final String KEYCODE_RSTICK_RIGHT = "rstick_right";
        public static final String KEYCODE_RSTICK_CENTER = "rstick_center";

        public static final String KEYCODE_SELECT = "select";
        public static final String KEYCODE_START = "start";

        public static final String STANDARD_JOYSTICK_KEYMAP_STRING = "{\"a\":96,\"b\":97,\"dpad_down\":20,\"dpad_left\":21,\"dpad_right\":22,\"dpad_up\":19,\"lb\":102,\"lstick_center\":106,\"lstick_down\":20,\"lstick_left\":21,\"lstick_right\":22,\"lstick_up\":19,\"lt\":104,\"rb\":103,\"rstick_center\":-1,\"rstick_down\":-1,\"rstick_left\":-1,\"rstick_right\":-1,\"rstick_up\":-1,\"rt\":105,\"select\":109,\"start\":108,\"x\":99,\"y\":100}";

        private Map<String, Integer> map = null;
        public String deviceID = null;
        public String productID = null;
        public String keyMap = null;

        @SuppressWarnings("unchecked")
        public JoyStickDevice(String deviceID, String productID) {
            this.deviceID = deviceID;
            this.productID = productID;
            keyMap = STANDARD_JOYSTICK_KEYMAP_STRING;

            map = (Map<String, Integer>) JSON.parseObject(keyMap, Map.class);
        }

        @SuppressWarnings("unchecked")
        public JoyStickDevice(String deviceID, String productID, String keyMap) {
            this.deviceID = deviceID;
            this.productID = productID;
            this.keyMap = keyMap;
            map = (Map<String, Integer>) JSON.parseObject(keyMap, Map.class);
        }

        public final boolean handleKey(String key, int keyCode) {
            Integer k = map.get(key);
            if (k != null)
                return k == keyCode;
            return false;
        }
    }
}
